using UnityEngine;
using System.Collections;
using UnityEditor;



[CustomEditor(typeof(Grid))]
public class GridEditor : Editor 
{
    public float m_GridSize = 1.0f;
    public Vector3 m_GridDimension;

    Vector3 m_ActualGridDimension;

    public bool m_GridCreated;
    public Grid m_Grid;
     GameObject m_GameObject;
    public bool mouseButtonPushed;

    GameObject go;
    GridElement[,] elements;
    GameObject m_Tile;
    GridEditor[] grids;


    public void OnEnable()
    {
        m_Grid =((Grid) target);
        m_GridDimension = new Vector3(1.0f, 1.0f, 1.0f);
        m_GameObject = (GameObject)(Resources.Load("Editor/Prefabs/Tile00"));
        m_Tile =(GameObject) (Resources.Load("Editor/Prefabs/GridElement"));
        GameObject go = m_GameObject;

    }

    /*! @brief : Clear the grid from all his child (tiles)
     */
    
    public void ClearGrid()
    {
		int childCount=m_Grid.transform.childCount;
		
		while(m_Grid.transform.childCount!=0)
		{
			DestroyImmediate(m_Grid.transform.GetChild(0).gameObject);
		}
    }

    /*! @brief : Generate a 3D grid using the inspector's user specified values
     * 
     */

    public void GenerateGrid()
    {
        ClearGrid();

        elements = new GridElement[(int)m_GridDimension.x, (int)m_GridDimension.y];
        m_ActualGridDimension = new Vector3(m_GridDimension.x, m_GridDimension.y, m_GridDimension.z); ;

       /* for (int i = 0; i < m_GridDimension.x; i++)
        {
            for (int j = 0; j < m_GridDimension.y; j++)
            {
                    elements[i, j] = ((GameObject)GameObject.Instantiate(m_Tile)).GetComponent<GridElement>();
                    elements[i, j].transform.position = new Vector3(i * m_GridSize,0.0f, j * m_GridSize);
                    elements[i, j].transform.localScale = new Vector3(m_GridSize * 0.5f, m_GridSize * 0.5f, m_GridSize * 0.5f);
                    elements[i, j].transform.parent = m_Grid.transform;
            }
        }*/
    }

    public override void OnInspectorGUI()
    {
        m_GridDimension = EditorGUILayout.Vector3Field(" Grid Size", m_GridDimension);
        m_GridSize = EditorGUILayout.FloatField(m_GridSize);
       
        if (GUILayout.Button("Generate Grid"))
        {
            GenerateGrid();
        }

        EditorGUILayout.SelectableLabel("objectType");

        if(GUILayout.Button("^"))
        {
            m_Grid.transform.position = new Vector3(m_Grid.transform.position.x, m_Grid.transform.position.y +1.0f, m_Grid.transform.position.z);
        }
        if (GUILayout.Button("down"))
        {
            m_Grid.transform.position = new Vector3(m_Grid.transform.position.x, m_Grid.transform.position.y - 1.0f, m_Grid.transform.position.z);
        }

        go=(GameObject)EditorGUILayout.ObjectField(" GameObject", go, typeof(GameObject));
    }

    public void Update()
    {
        Event.current.Use();
    }

    public void OnSceneGUI()
    {
		Event sceneEvent = Event.current;
	
       // int controlID = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);

        // Use this in order to DONT LOSE CONTROL OF THE INSPECTOR
        if (!Selection.Contains(this.m_Grid.gameObject))
        {
            Selection.activeGameObject = this.m_Grid.gameObject;
        }

     /*   if (sceneEvent.type == EventType.MouseDown)
        {
            Debug.Log(this.m_Grid.gameObject.name);
            
        }*/
          /*  if (sceneEvent.button == 0)
            {
                float yvalue;
                yvalue = sceneEvent.mousePosition.y;

                Ray ray = Camera.current.ScreenPointToRay(new Vector2(sceneEvent.mousePosition.x, Screen.height - 40.0f - yvalue));
                RaycastHit hit = new RaycastHit();

                if (Physics.Raycast(ray, out hit, 1000.0f))
                {
                    GridElement els;

                    if (els = hit.transform.gameObject.GetComponent<GridElement>())
                    {
                            GameObject test = (GameObject) GameObject.Instantiate(go);
                            Debug.Log(els.m_Useable);
                            test.transform.position += hit.transform.position;
                            els.m_Useable = false;
                    }
                }
               // Event.current.Use();
            }
           
        }

        if (sceneEvent.type== EventType.KeyDown)
        {
            if (sceneEvent.keyCode == KeyCode.T)
            {
                m_Grid.transform.position = new Vector3(m_Grid.transform.position.x, m_Grid.transform.position.y - 1.0f, m_Grid.transform.position.z);
       
            }
            else if (sceneEvent.keyCode == KeyCode.G)
            {
                m_Grid.transform.position = new Vector3(m_Grid.transform.position.x, m_Grid.transform.position.y + 1.0f, m_Grid.transform.position.z);
       
            }
        }*/
  

        //HandleUtility.AddDefaultControl(controlID);
    }
}
